$(function () {
    initTreeTable();
});

function initTreeTable() {
    var setting = {
        id: 'menuId',
        code: 'menuId',
        url: ctx + 'menu/listtable',
        expandAll: false,
        expandColumn: "2",
        ajaxParams: {
            menuName: ""
        },
        columns: [
            {
                field: 'selectItem',
                checkbox: true
            },
            {
                title: '编号',
                field: 'menuId',
                width: '50px'
            },
            {
                title: '名称',
                field: 'menuName'
            },

            {
                title: '图标',
                field: 'icon',
                formatter: function (item, index) {
                    return '<i class="zmdi ' + item.icon + '"></i>';
                }
            },
            {
                title: '类型',
                field: 'type',
                formatter: function (item, index) {
                    if (item.type === '0') return '<span class="badge badge-success">菜单</span>';
                    if (item.type === '1') return '<span class="badge badge-warning">按钮</span>';
                }
            },
            {
                title: '地址',
                field: 'url',
                formatter: function (item, index) {
                    return item.url === 'null' ? '' : item.url;
                }
            },
            {
                title: '权限标识',
                field: 'perms',
                formatter: function (item, index) {
                    return item.perms === 'null' ? '' : item.perms;
                }
            },
            {
                title: '创建时间',
                field: 'createTime'
            }
        ]
    };
    $JuXue.initTreeTable('menu-data-table', setting);
}
/**刷新菜单**/
function Menu_refresh() {
    initTreeTable();
    //$JuXue.refreshJsTree("menuTree", createMenuTree());
}
/**删除菜单**/
function deleteMenus() {
    var ids = $("#menu-data-table").bootstrapTreeTable("getSelections");
    var ids_arr = "";
    if (!ids.length) {$JuXue.n_warning("请勾选需要删除的菜单或按钮！");return;}
    for (var i = 0; i < ids.length; i++) {
        ids_arr += ids[i].id;
        if (i !== (ids.length - 1)) ids_arr += ",";
    }
    $JuXue.confirm({
        text: "确定删除选中菜单或按钮？",
        confirmButtonText: "确定删除"
    }, function () {
        $.post(ctx + 'menu/delete', {"ids": ids_arr}, function (r) {
            if (r.code === 0) {
                $JuXue.n_success(r.msg);
                Menu_refresh();
            } else {
                $JuXue.n_danger(r.msg);
            }
        });
    });
}
/**导出菜单栏**/
function exportMenuExcel() {
    $.post(ctx + "menu/excel", $(".menu-table-form").serialize(), function (r) {
        if (r.code === 0) {
            window.location.href = "file/download?fileName=" + r.msg + "&delete=" + true;
        } else {
            $JuXue.n_warning(r.msg);
        }
    });
}

/**新增菜单栏**/
function addMenu(){
    $.post(ctx+"menu/addDialog",{},function (r) {$("#menu_info").empty().append(r).modal("show").draggable();});
}
/**修改菜单栏**/
function editMenu() {
    var selected = $("#menu-data-table").bootstrapTreeTable("getSelections");
    var selected_length = selected.length;
    if (!selected_length) {$JuXue.n_warning('请勾选需要修改的菜单或按钮！');return;}
    if (selected_length > 1) {$JuXue.n_warning('一次只能修改一个菜单或按钮！');return;}
    $.post(ctx+"menu/editDialog",{},function (r) {$("#menu_info").empty().append(r).modal("show").draggable();});
}